package com.tdkj.action.backstage.risevalue;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.risevalue.BeanAdvert;
import com.tdkj.model.risevalue.BeanAdvertAuctionBidInfo;
import com.tdkj.model.risevalue.BeanAdvertAuctionInfo;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.risevalue.IBeanAdvertAuctionService;
public class AdvertAuctionAction extends BaseAction {
	private Long id;//广告位id
	private Long aid;//广告位竞价id
	private Long bid;//广告位竞价明细id
	private String fullId;//tree查询时候的id
	private String fileurl;//文件位置
	private BeanAdvertAuctionInfo auctioninfo;
	private IBeanAdvertAuctionService advertAuctionService;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getAid() {
		return aid;
	}
	public void setAid(Long aid) {
		this.aid = aid;
	}
	public Long getBid() {
		return bid;
	}
	public void setBid(Long bid) {
		this.bid = bid;
	}
	public String getFullId() {
		return fullId;
	}
	public String getFileurl() {
		return fileurl;
	}
	public void setFileurl(String fileurl) {
		this.fileurl = fileurl;
	}
	public void setFullId(String fullId) {
		this.fullId = fullId;
	}
	public BeanAdvertAuctionInfo getAuctioninfo() {
		return auctioninfo;
	}
	public void setAuctioninfo(BeanAdvertAuctionInfo auctioninfo) {
		this.auctioninfo = auctioninfo;
	}
	public IBeanAdvertAuctionService getAdvertAuctionService() {
		return advertAuctionService;
	}
	@Resource
	public void setAdvertAuctionService(
			IBeanAdvertAuctionService advertAuctionService) {
		this.advertAuctionService = advertAuctionService;
	}
	
	//获取首页
	public String findAll(){
		return "main";
	}
	//获取广告位竞价
	public String findAuctionAudits(){
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.add(Restrictions.isNotNull("fileurl"));
		criteria.addOrder(Order.desc("aendtime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAuctionAction!findAuctionAudits.action", 10);
		return "au_audit";
	}
	//设置为审核
	public String updateAuditstatus(){
		BeanAdvertAuctionInfo bean=this.getBaseService().findById(this.getAid(), BeanAdvertAuctionInfo.class);
		bean.setAuditstatus("已审核");
		this.getBaseService().saveOrUpdate(bean, BeanAdvertAuctionInfo.class);
		return findAuctionAudits();
	}
	//查看文件
	public String findFileurl(){
		auctioninfo=this.getBaseService().findById(this.getAid(), BeanAdvertAuctionInfo.class);
		return "au_file";
	}
	
	//获取广告位
	public String findAdverts(){
		if(fullId!=null){
			String hql = "select bean from " + BeanAdvert.class.getName() +" as bean, " + BeanType.class.getName() + " as type "
			+ " where bean.pageid=type.id and (type.fullId='"+fullId+"' or type.fullId like '"+fullId+"-%')" 
			+ " order by pagename desc";
			Query query = this.createQuery(hql);
			this.queryByPage(query, "advertAction!findList.action", 10);
		}else{
			Criteria criteria = this.createCriteria(BeanAdvert.class);
			criteria.addOrder(Order.desc("name"));// 对状态进行排序
			this.queryByPage(criteria, "advertAuctionAction!findAdverts.action", 10);
		}
		return "ad_list";
	}
	//获取广告位竞价信息
	public String findAuctions(){
		BeanAdvert adbean=this.getBaseService().findById(this.getId(), BeanAdvert.class);
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.add(Restrictions.eq("advert",adbean));
		criteria.addOrder(Order.desc("aendtime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAuctionAction!findAuctions.action", 10);
		return "au_list";
	}
	//获取广告位竞价明细
	public String findBids(){
		BeanAdvertAuctionInfo aubean=this.getBaseService().findById(this.getAid(), BeanAdvertAuctionInfo.class);
		Criteria criteria = this.createCriteria(BeanAdvertAuctionBidInfo.class);
		criteria.add(Restrictions.eq("auctioninfo",aubean));
		criteria.addOrder(Order.desc("auctiontime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAuctionAction!findBids.action", 10);
		return "bi_list";
	}
	
	//设置为竞价成功
	public String updateAuction(){
		aid=this.getAid();
		BeanAdvertAuctionInfo auctioninfo=this.getBaseService().findById(this.getAid(), BeanAdvertAuctionInfo.class);
		BeanAdvertAuctionBidInfo bidinfo=this.getBaseService().findById(this.getBid(), BeanAdvertAuctionBidInfo.class);
		//扣款
		//取消其他的款项冻结
		
		//开始发送站内信息
		StringBuffer sb=new StringBuffer();
		sb.append("您已经竞价成功!请在规定时间内，付款！您获得的广告位编号是:"+auctioninfo.getAdvert().getCode());
		sb.append("支付金额为"+bidinfo.getPrice());
		sb.append("/"+auctioninfo.getCurrency());
		addMessage(bidinfo.getUser().getId(),sb.toString());
		//结束发送站内信息
		
		auctioninfo.setEndprice(bidinfo.getPrice());
		auctioninfo.setStatus("竞价结束");
		auctioninfo.setAuctiontime(bidinfo.getAuctiontime());
		bidinfo.setStatus("竞价成功");
		this.getBaseService().saveOrUpdate(bidinfo, BeanAdvertAuctionBidInfo.class);
		this.getBaseService().saveOrUpdate(auctioninfo, BeanAdvertAuctionInfo.class);
		return findBids();
	}
	
	//发送短消息
	private  void addMessage(Long uid,String content){
		BeanUser ru=this.getBaseService().findById(uid,BeanUser.class);
		BeanMessage bean=new BeanMessage();
		bean.setContent(content);
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		bean.setMsgtype(0);
		bean.setBeanAdmin(this.getCurrentAdmin());
		bean.setReciver(ru);
		this.getBaseService().save(bean);
	}
	
	
	
}
